

# Receive post information from buidaschedule, make a calendar, and put it in 
# the datastore
# Directs admin to add users to the datastore

import webapp2
from google.appengine.ext import db

from google.appengine.api import mail

import ext


class MainPage(ext.SlotHandler):
	def post(self):
		# Collect the variables
		if True: 
			b1_1 = self.request.get('b1_1')	
			b1_2 = self.request.get('b1_2')
			b1_3 = self.request.get('b1_3')
			b1_4 = self.request.get('b1_4')
			b1_5 = self.request.get('b1_5')
			b1_6 = self.request.get('b1_6')
			b1_7 = self.request.get('b1_7')
			b1_8 = self.request.get('b1_8')
			b1_9 = self.request.get('b1_9')
			b1_10 = self.request.get('b1_10')
			b1_11 = self.request.get('b1_11')
			b1_12 = self.request.get('b1_12')
			b1_13 = self.request.get('b1_13')
			b1_14 = self.request.get('b1_14')
			b1_15 = self.request.get('b1_15')
			b1_16 = self.request.get('b1_16')
			b1 = [b1_1, b1_2, b1_3, b1_4, b1_5, b1_6, b1_7, b1_8, b1_9, b1_10, b1_11, b1_12, b1_13, b1_14, b1_15, b1_16]
			
			b2_1 = self.request.get('b2_1')	
			b2_2 = self.request.get('b2_2')
			b2_3 = self.request.get('b2_3')
			b2_4 = self.request.get('b2_4')
			b2_5 = self.request.get('b2_5')
			b2_6 = self.request.get('b2_6')
			b2_7 = self.request.get('b2_7')
			b2_8 = self.request.get('b2_8')
			b2_9 = self.request.get('b2_9')
			b2_10 = self.request.get('b2_10')
			b2_11 = self.request.get('b2_11')
			b2_12 = self.request.get('b2_12')
			b2_13 = self.request.get('b2_13')
			b2_14 = self.request.get('b2_14')
			b2_15 = self.request.get('b2_15')
			b2_16 = self.request.get('b2_16')
			b2 = [b2_1, b2_2, b2_3, b2_4, b2_5, b2_6, b2_7, b2_8, b2_9, b2_10, b2_11, b2_12, b2_13, b2_14, b2_15, b2_16]
			
			b3_1 = self.request.get('b3_1')	
			b3_2 = self.request.get('b3_2')
			b3_3 = self.request.get('b3_3')
			b3_4 = self.request.get('b3_4')
			b3_5 = self.request.get('b3_5')
			b3_6 = self.request.get('b3_6')
			b3_7 = self.request.get('b3_7')
			b3_8 = self.request.get('b3_8')
			b3_9 = self.request.get('b3_9')
			b3_10 = self.request.get('b3_10')
			b3_11 = self.request.get('b3_11')
			b3_12 = self.request.get('b3_12')
			b3_13 = self.request.get('b3_13')
			b3_14 = self.request.get('b3_14')
			b3_15 = self.request.get('b3_15')
			b3_16 = self.request.get('b3_16')
			b3 = [b3_1, b3_2, b3_3, b3_4, b3_5, b3_6, b3_7, b3_8, b3_9, b3_10, b3_11, b3_12, b3_13, b3_14, b3_15, b3_16]
			
			b4_1 = self.request.get('b4_1')	
			b4_2 = self.request.get('b4_2')
			b4_3 = self.request.get('b4_3')
			b4_4 = self.request.get('b4_4')
			b4_5 = self.request.get('b4_5')
			b4_6 = self.request.get('b4_6')
			b4_7 = self.request.get('b4_7')
			b4_8 = self.request.get('b4_8')
			b4_9 = self.request.get('b4_9')
			b4_10 = self.request.get('b4_10')
			b4_11 = self.request.get('b4_11')
			b4_12 = self.request.get('b4_12')
			b4_13 = self.request.get('b4_13')
			b4_14 = self.request.get('b4_14')
			b4_15 = self.request.get('b4_15')
			b4_16 = self.request.get('b4_16')
			b4 = [b4_1, b4_2, b4_3, b4_4, b4_5, b4_6, b4_7, b4_8, b4_9, b4_10, b4_11, b4_12, b4_13, b4_14, b4_15, b4_16]
			
			b5_1 = self.request.get('b5_1')	
			b5_2 = self.request.get('b5_2')
			b5_3 = self.request.get('b5_3')
			b5_4 = self.request.get('b5_4')
			b5_5 = self.request.get('b5_5')
			b5_6 = self.request.get('b5_6')
			b5_7 = self.request.get('b5_7')
			b5_8 = self.request.get('b5_8')
			b5_9 = self.request.get('b5_9')
			b5_10 = self.request.get('b5_10')
			b5_11 = self.request.get('b5_11')
			b5_12 = self.request.get('b5_12')
			b5_13 = self.request.get('b5_13')
			b5_14 = self.request.get('b5_14')
			b5_15 = self.request.get('b5_15')
			b5_16 = self.request.get('b5_16')
			b5 = [b5_1, b5_2, b5_3, b5_4, b5_5, b5_6, b5_7, b5_8, b5_9, b5_10, b5_11, b5_12, b5_13, b5_14, b5_15, b5_16]
			
			b6_1 = self.request.get('b6_1')	
			b6_2 = self.request.get('b6_2')
			b6_3 = self.request.get('b6_3')
			b6_4 = self.request.get('b6_4')
			b6_5 = self.request.get('b6_5')
			b6_6 = self.request.get('b6_6')
			b6_7 = self.request.get('b6_7')
			b6_8 = self.request.get('b6_8')
			b6_9 = self.request.get('b6_9')
			b6_10 = self.request.get('b6_10')
			b6_11 = self.request.get('b6_11')
			b6_12 = self.request.get('b6_12')
			b6_13 = self.request.get('b6_13')
			b6_14 = self.request.get('b6_14')
			b6_15 = self.request.get('b6_15')
			b6_16 = self.request.get('b6_16')
			b6 = [b6_1, b6_2, b6_3, b6_4, b6_5, b6_6, b6_7, b6_8, b6_9, b6_10, b6_11, b6_12, b6_13, b6_14, b6_15, b6_16]
			
			b7_1 = self.request.get('b7_1')	
			b7_2 = self.request.get('b7_2')
			b7_3 = self.request.get('b7_3')
			b7_4 = self.request.get('b7_4')
			b7_5 = self.request.get('b7_5')
			b7_6 = self.request.get('b7_6')
			b7_7 = self.request.get('b7_7')
			b7_8 = self.request.get('b7_8')
			b7_9 = self.request.get('b7_9')
			b7_10 = self.request.get('b7_10')
			b7_11 = self.request.get('b7_11')
			b7_12 = self.request.get('b7_12')
			b7_13 = self.request.get('b7_13')
			b7_14 = self.request.get('b7_14')
			b7_15 = self.request.get('b7_15')
			b7_16 = self.request.get('b7_16')
			b7 = [b7_1, b7_2, b7_3, b7_4, b7_5, b7_6, b7_7, b7_8, b7_9, b7_10, b7_11, b7_12, b7_13, b7_14, b7_15, b7_16]
		
		ac = ext.Account.gql("WHERE email = :1", self.request.get('email')).get()
		if ac is None:
			ac = ext.Account(email=self.request.get('email'))
			ac.put()
		
		## Create the shifts that exist and add the shifts to the datastore and calendar
		for i in range(16):
			t = 8 + i
			if b1[i]:
				sh = ext.Shift.gql("WHERE day = :1 AND time = :2", 1,t).get()
				if sh:
					ext.addAvail(ac, sh)
			if b2[i]:
				sh = ext.Shift.gql("WHERE day = :1 AND time = :2", 2,t).get()
				if sh:
					ext.addAvail(ac, sh)
			if b3[i]:
				sh = ext.Shift.gql("WHERE day = :1 AND time = :2", 3,t).get()
				if sh:
					ext.addAvail(ac, sh)
			if b4[i]:
				sh = ext.Shift.gql("WHERE day = :1 AND time = :2", 4,t).get()
				if sh:
					ext.addAvail(ac, sh)
			if b5[i]:
				sh = ext.Shift.gql("WHERE day = :1 AND time = :2", 5,t).get()
				if sh:
					ext.addAvail(ac, sh)
			if b6[i]:
				sh = ext.Shift.gql("WHERE day = :1 AND time = :2", 6,t).get()
				if sh:
					ext.addAvail(ac, sh)
			if b7[i]:
				sh = ext.Shift.gql("WHERE day = :1 AND time = :2", 7,t).get()
				if sh:
					ext.addAvail(ac, sh)
				
		self.response.out.write(open('html/header-submitAvail.html','r').read())
		self.response.out.write('<div align="center">')
		self.response.out.write("<h1>Hi "+ self.request.get('email') + "! Your availability has been added!</h1>")
		self.response.out.write('</div>')
		self.response.out.write(open('html/footer-addUsers.html','r').read())
		
		mail.send_mail(sender="slotme.n.help@gmail.com",
					to=ac.email,
					subject="Your account has been approved",
					body="""Congrats. You have submitted your availability! You can view your calendar here: slotme-n.appspot.com/userCal ?email=""" +ac.email + """.""")
		
app = webapp2.WSGIApplication([('/submitAvail', MainPage)], debug=True)